package com.baizhi.controller;

import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("selectUserByPage")
    @ResponseBody
    public Map selectUserByPage(int page, int rows){

        return userService.selectUserByPage(page, rows);
    }

    @RequestMapping("add")
    @ResponseBody
    public Map add(User user){
        Map map=new HashMap();
        try {
            userService.insert(user);
            map.put("isAdd",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("isAdd",false);
        }
        return map;
    }

    @RequestMapping("update")
    @ResponseBody
    public Map update(User user){
        Map map=new HashMap();
        try {
            userService.update(user);
            map.put("isUpdate",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("isUpdate",false);
        }
        return map;
    }

    @RequestMapping("delete")
    @ResponseBody
    public Map delete(int ids[]){
        Map map=new HashMap();
        try {
            userService.delete(ids);
            map.put("isDelete",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("isDelete",false);
        }
        return map;
    }

}
